﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Nova_DAL;
using Nova_BLL;
using Nova_Common;
using System.Data.Common;
using System.Text.RegularExpressions;
using Nova_Common.Extend;
using Nova_MOD;
using System.Collections.Generic;

public partial class Material_AddMaterial : System.Web.UI.Page
{
    DbHelper dbHelper = new DbHelper();
    DbCommand cmd = null;
    string strSQL = "";
    int i = 0;

    Nova_DAL.MaterialType Nova_BLL_MaterialType = new Nova_DAL.MaterialType();
    TMaterialGroupInfoBll MaterialGroupBll = new TMaterialGroupInfoBll();
    string strMessage = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind_ddlParentType();
        }
    }
    /// <summary>
    /// 绑定物料归属
    /// </summary>
    private void Bind_ddlParentType()
    {
        try
        {
            List<TMaterialGroupInfo> list = MaterialGroupBll.GetList();
            DataTable dt = TableList.ToDataTable(list);
            ddlGroupType.DataSource = dt;
            ddlGroupType.DataBind();
            ddlGroupType.Items.Insert(0, new ListItem("====请选择====", "-1"));
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }

    /// <summary>
    /// 父类索引改变
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlParentType_SelectedIndexChanged(object sender, EventArgs e)
    {
      //  Bind_ddlSubType(ddlParentType.SelectedValue);
        this.txtMat_Code.Text = "";
    }

    /// <summary>
    /// 清空ddl绑定信息
    /// </summary>
    /// <param name="ddl"></param>
    protected void ClearDropDown(DropDownList ddl)
    {
        ddl.Items.Clear();
        ddl.Items.Insert(0, new ListItem("====请选择====", "-1"));
    }

    /// <summary>
    /// 确认新增操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnOK_Click(object sender, ImageClickEventArgs e)
    {
        //非空判断
        if (ddlGroupType.SelectedIndex == 0)
        {
            ShowBox.MessageBox(this.Page, "请选择物料频率");
            return;
        }
       
        if (string.IsNullOrEmpty(this.txtMat_Code.Text.Trim()))
        {
            ShowBox.MessageBox(this.Page, "物料编码不能为空");
            return;
        }
        if (!CheckPara(txtMiniNum.Text, out strMessage))
        {
            ShowBox.MessageBox(this.Page, strMessage);
            return;
        }

        if (iMaterial(txtMat_Code.Text) == 0)
        {
            strSQL = "INSERT INTO TBase_Material(Mat_Name,Mat_Code,Unit_Base,S01,S02,S04,TM01,TM03,MiniNum,GroupType,OID) values('" + txtMat_Name.Text + "','" + txtMat_Code.Text + "','" + txtUnit_Base.Text + "','" + txtS01.Text + "','" + txtS02.Text + "','" + txtS04.Text + "','" + txtTM01.Text + "','" + txtTM03.Text + "','" + Convert.ToDecimal(txtMiniNum.Text) + "','" + ddlGroupType.SelectedValue + "','WMS')";
            cmd = dbHelper.GetSqlStringCommond(strSQL);
            i = dbHelper.ExecuteNonQuery(cmd);
            if (i > 0)
            {
                ShowBox.MessageBox(this.Page, "操作成功！");
                DelDate();
                ibtnClose_Click(sender, e);
            }
            else
            {
                ShowBox.MessageBox(this.Page, "操作失败！");
            }
        }
        else
        {
            ShowBox.MessageBox(this.Page, "该物料已经存在，请到修改页面操作！");
        }
    }


    protected void DelDate()
    {
        txtMat_Code.Text = "";
        txtMat_Name.Text = "";
        txtS01.Text = "";
        txtS02.Text = "";
        txtUnit_Base.Text = "";

        txtS04.Text = "";
        //txtS06.Text = "";
        txtMiniNum.Text = "";
        ddlGroupType.SelectedIndex = 0;
    }

    protected void ibtnClose_Click(object sender, ImageClickEventArgs e)
    {
        //this.Page.ClientScript.RegisterStartupScript(typeof(string), "Nova1", "window.returnValue='1';top.close();", true);
        this.Page.ClientScript.RegisterStartupScript(typeof(string), "Nova1", "window.opener.document.getElementById('hidden').value='1';window.opener.document.getElementById('form1').submit();top.close();", true);
    }

    protected int iMaterial(string strMAT_CODE)
    {
        int iMaterial = 0;
        strSQL = "SELECT MAT_CODE FROM TBase_Material WHERE MAT_CODE='" + strMAT_CODE + "'";
        cmd = dbHelper.GetSqlStringCommond(strSQL);
        DataSet ds_M = dbHelper.ExecuteDataSet(cmd);
        if (ds_M != null && ds_M.Tables[0].Rows.Count > 0)
        {
            iMaterial = 1;
        }
        return iMaterial;
    }

    protected bool CheckPara(string strNum, out string strMessage)
    {
        strMessage = "";
        string word = @"^\d+(\.\d+)?$";
        Match Ma = Regex.Match(strNum, word);
        if (Ma.Success)
        {
            return true;
        }
        else
        {
            strMessage = "最低限值必须为正数字！";
            return false;
        }
    }

}
